//
//  LXCooperationListViewController.m
//  ZuLinProject
//
//  Created by bancheng on 2021/12/27.
//  Copyright © 2021 LX_App. All rights reserved.
//

#import "LXCooperationListViewController.h"
#import "LXRepairDepotTableViewCell.h"
#import "LXCooperationModel.h"
#import "LXCooperationtApplyViewController.h"

@interface LXCooperationListViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic) UITableView *tableView;
@property (nonatomic) NSMutableArray *data;
@property (nonatomic) NSInteger pageIndex;
@property (nonatomic) LXCommSearchView *searchView;
@end

@implementation LXCooperationListViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    
    if (kUserDefaultForBool(kRefreshStr)) {
        kUserDefaultSetBool(NO, kRefreshStr);
        [self.tableView.mj_header beginRefreshing];
    }
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
   
    if (self.type == 1) {
        [self layoutCustomNav:self.view title:@"合作修理厂"];
    }
    else if (self.type == 2){
        [self layoutCustomNav:self.view title:@"合作公司"];
    }
    else {
        [self layoutCustomNav:self.view title:@"合作公司"];
    }
    
    if (self.type != 0) {
        UIButton *rightButton = [[UIButton alloc] initWithFrame:CGRectMake(kScreenWidth - 70, KStatuesBarHeight, 60, 40)];
       rightButton.titleLabel.font = [UIFont systemFontOfSize:14];
       [rightButton setTitle:@"新增" forState:UIControlStateNormal];
        [rightButton setImage:Image(@"icon_add") forState:UIControlStateNormal];
       [rightButton setTitleColor:mainColor forState:UIControlStateNormal];
        [rightButton addTarget:self action:@selector(clickBtn) forControlEvents:UIControlEventTouchUpInside];
       [self.view addSubview:rightButton];
    }
   
    
    [self.view addSubview:self.tableView];
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([LXRepairDepotTableViewCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([LXRepairDepotTableViewCell class])];
    
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.pageIndex = 1;
        [self loadData];
    }];

    [self.tableView.mj_header beginRefreshing];
    self.tableView.mj_footer = [MJRefreshBackStateFooter footerWithRefreshingBlock:^{
         self.pageIndex++;
        [self loadData];
    }];
    
  

   
    
}

- (void)loadData{
    
    NSDictionary *dic = @{};
    if (self.type != 0) {
        dic = @{
            @"page":@(self.pageIndex),
            @"rows":@(20),
            @"type":@(self.type)
        };
    }

    [MSNetworkManager getRevertUrl:GET_GetThirdpartycompany params:dic success:^(id  _Nonnull paginationObj, id  _Nonnull resonseObj, BOOL isSuccess, NSString * _Nonnull message) {
        
        [self endRefreshing];
        
        if ([self.tableView.mj_header isRefreshing]||self.pageIndex==1) {
            [self.data removeAllObjects];
            [self.tableView.mj_footer resetNoMoreData];
        }
        NSInteger totalPage = [resonseObj[@"total"] integerValue];
            
        NSArray *records = resonseObj[@"records"];
        
        [self.data addObjectsFromArray:[LXCooperationModel  mj_objectArrayWithKeyValuesArray:records]];
        
        
        if (self.data.count>=totalPage) {
            self.tableView.mj_footer.state = MJRefreshStateNoMoreData;
        }
        else {
            self.tableView.mj_footer.state = MJRefreshStateIdle;
        }
        
            
        [self.tableView reloadData];
            
        [self isHaveEmptyData];
            
       
    } failure:^(NSUInteger code, NSString * _Nonnull message) {
        [self endRefreshing];
        [self isHaveEmptyData];
    } withShowHud:YES];

}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.data.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    LXRepairDepotTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([LXRepairDepotTableViewCell class])];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell setClipsToBounds:YES];

    LXCooperationModel *model = self.data[indexPath.row];
    cell.model = model;
    if (self.type == 2) {
        cell.label4.hidden = YES;
        cell.key4Lab.hidden = YES;
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (self.clickVcCallBlock) {
        LXCooperationModel *model = self.data[indexPath.row];
        self.clickVcCallBlock(model);
        [self.navigationController popViewControllerAnimated:YES];
    }
    
}





- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, kViewTopHeight, kScreenWidth, kScreenHeight - kViewTopHeight) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = 150;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = ColorWithF7F8FA;
        _tableView.tableFooterView = [UIView new];
    }
    return _tableView;
}

- (void)isHaveEmptyData{
    if (self.data.count == 0) {
        self.tableView.startTip = YES;
        self.tableView.tipView.backgroundColor = self.tableView.backgroundColor;
        self.tableView.tipImage = [UIImage imageNamed:@"No_order_icon"];
        self.tableView.tipView.hidden = NO;
        self.tableView.scrollEnabled = YES;
    }else{
        self.tableView.tipView.hidden = YES;
        self.tableView.scrollEnabled = YES;
    }
}


- (void)endRefreshing{
    if ([self.tableView.mj_header isRefreshing]) {
        [self.tableView.mj_header endRefreshing];
        self.tableView.mj_header.state = MJRefreshStateIdle;
        [self.tableView.mj_footer resetNoMoreData];
    }
    if ([self.tableView.mj_footer isRefreshing]) {
        [self.tableView.mj_footer endRefreshing];
    }

}
- (NSMutableArray *)data{
    if (!_data) {
        _data = [[NSMutableArray alloc]init];
    }
    return _data;
}

// 新增保险
-(void)clickBtn{
   
    LXCooperationtApplyViewController *vc = [[LXCooperationtApplyViewController alloc] init];
    vc.type = self.type;
    [self.navigationController pushViewController:vc animated:YES];
}

@end
